import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/expert/plan_form_detail_logic.dart';
import 'package:sports_new/entity/expert/match_play_entity.dart';
import 'package:sports_new/entity/expert/plan_form/plan_form_detail_entity.dart';
import 'package:sports_new/entity/match/expert_views_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/view/expert/viewpoint/match_play_view.dart';
import 'package:sports_new/view/king/items/history_label.dart';
import 'package:sports_new/widgets/card.dart';
import 'package:sports_new/widgets/base/qkd_image.dart';

import '../../../widgets/base/qkd_text.dart';

class PlanFormDetailPage extends StatefulWidget {
  const PlanFormDetailPage({super.key});

  @override
  State<PlanFormDetailPage> createState() => _PlanFormDetailPageState();
}

class _PlanFormDetailPageState extends State<PlanFormDetailPage> {
  final PlanFormDetailLogic logic = Get.put(PlanFormDetailLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PlanFormDetailLogic>(
      builder: (logic) {
        return Scaffold(
          backgroundColor: Colours.greyF2F3F7,
          appBar: Styles.appBar(title: const QkdText("计划详情")),
          body: Container(
            decoration: const BoxDecoration(
              border: Border(top: BorderSide(color: Colours.greyEE,width: 0.5))
            ),
            child: SingleChildScrollView(
              child: Column(
                children: [
                  Container(height: 15),
                  Row(
                    children: [
                      ClipOval(
                        child: QkdImage(
                          imageUrl: logic.data?.expertLogo, width: 40, height: 40
                        ),
                      ),
                      Container(width: 8),
                      QkdText(logic.data?.expertName ?? "",
                        fontWeight: FontWeight.w500
                      )
                      // Container(width: 5),
                      // if(logic.issueInfo?.buy.hasValue == true)
                      // SizedBox(
                      //   height: 17,
                      //   child: Stack(
                      //     alignment: AlignmentDirectional.center,
                      //     children: [
                      //       Positioned.fill(
                      //         child: Image.asset(Utils.getImgPath("plan_buy_tag.png"),fit: BoxFit.fill)
                      //       ),
                      //       QkdText("${logic.issueInfo?.buy}人已购买",
                      //         strutStyle: Styles.centerStyle(fontSize: 12),
                      //         style: const TextStyle(fontSize: 12,color: Colours.white)
                      //       ).paddingOnly(left: 13,right: 4),
                      //     ],
                      //   ),
                      // )
                    ],
                  ).paddingSymmetric(horizontal: 16),
                  Container(height: 18),
                  if(logic.issueInfo?.state == 2 || logic.issueInfo?.state == 3)
                  profitLabel().paddingSymmetric(horizontal: 16),
                  Column(
                    children: logic.issueInfo?.issueDetail?.map(
                      (e) => stage(e)
                    ).toList() ?? [],
                  )
                ]
              ),
            ),
          ),
        );
      }
    );
  }

  Widget stage(IssueDetail entity){
    return Block(
      cardStyle: const CardStyle(
        margin: EdgeInsets.fromLTRB(16, 8, 16, 0),
        color: Colours.white,
        radius: 10,
        shadow: BoxShadow(color: Colors.transparent)
      ),
      child: Stack(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.fromLTRB(16,10,0,10),
                child: QkdText("阶段${entity.stage.toChn}"),
              ),
              const Divider(color: Colours.greyEE,height: 0.5).paddingSymmetric(horizontal: 16),
              Container(height: 10),
              ...List<Widget>.generate(
                entity.matchBries?.length ?? 0, (index) {
                  var match = entity.matchBries?[index];
                  var itemList = entity.itemShow?.where(
                    (element) => element.matchId == match?.matchId
                  ).toList();
                  return item(
                    MatchPlayEntity.fromPlanForm(entity),
                    match
                  );
                }
              )
            ],
          ),
          Positioned(
            top: 16,
            right: 16,
            child: HistoryLabel.planFormDetail(state: entity.state)
          )
        ],
      ),
    );
  }

  Widget item(MatchPlayEntity? entity,MatchBries? match){
    return MatchPlayView(
      match: match,
      matchPlay: entity,
    );
  }

  Widget profitLabel(){
    return SizedBox(
      height: 36,
      child: Stack(
          alignment: AlignmentDirectional.center,
          children: [
            Container(
                height: 32,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Colours.white,
                    borderRadius: BorderRadius.circular(4)
                ),
                child: DefaultTextStyle(
                  style: const TextStyle(fontSize: 12,color: Colours.text),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      if(logic.issueInfo?.state == 2)...[
                        QkdText("本期综合回报率 ",needCenter: true),
                        QkdText("${((logic.issueInfo?.issueProfit ?? 0)*100).toInt()}%",
                          color: Colours.main
                        )
                      ]else...[
                        QkdText("本期未命中")
                      ]
                    ],
                  ),
                )
            ),
            Positioned(
                right: 32,
                child: HistoryLabel.planForm(state: logic.issueInfo?.state)
            )
          ]
      ),
    );
  }
}
